<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ImportController
 *
 * @author Regan
 */

App::uses('Folder', 'Utility');
App::uses('File', 'Utility');
App::import('Vendor', 'mdbimporter', array('file'=> 'MDBImport' . DS . 'MDBImporter.php'));
App::import('Vendor', 'importer', array('file'=> 'MDBImport' . DS . 'Importer.php'));
class ImportController extends AppController{
    public function index()
    {
        $path = '';
        $arrayTest = null;
        
        if($this->request->is('post'))
        {
            $parameter = $this->request->data;
            
            if($parameter['false']['type'] == 'PIB')
            {
                $filename = $parameter['false']['submittedfile']['name'];
                $explodedFilename = explode('.', $filename);
                $srcFile = new File($this->request->data['false']['submittedfile']['tmp_name']);
                $destFolder = new Folder('./../webroot/files/dataPIB/');
                $archiveFolder = new Folder('./../webroot/files/dataPIB/Archive');

                $extFile = $explodedFilename[count($explodedFilename)-1];
                
                if(strtoupper($extFile) == 'MDB' 
                        || strtoupper($extFile) == 'BUP')
                {              
                    $FilePIB = 'dbPIB.mdb';
                    $FileDest = $destFolder->pwd() . DS . $FilePIB;
                    $ProcessedBy = $this->Auth->user('username');
                    
                    $filenameNoExt = str_replace('.' . $extFile, '', $filename);
                    $ArchiveFileName = $filenameNoExt . '_' . date('YmdHis');
                    
                    $srcFile->copy($FileDest);
                    $this->PIBimport($FileDest, $FilePIB, $ProcessedBy, $ArchiveFileName, $filenameNoExt);
                    
                    $srcFile->copy($archiveFolder->pwd() . DS . $ArchiveFileName . '.' . $extFile);
                    $srcFile->delete();
                }
                else
                {
                    $path = 'Format File tidak cocok';
                }

            }
        }
        
        $this->set('test', $arrayTest);
        $this->set('check', $path);
    }
    
    public function PIBimportScheduler()
    {
        $FileDest = 'E:\xampp\htdocs\phpaccessdb\dbPIB.mdb';
        $FileDirectory = './../../../phpaccessdb/';
        $FileName = 'dbPIB.mdb';
        $filenameNoExt = '';
        $ProcessedBy = 'System';
        $ArchiveFileName = 'dbPib_201409010901';
        
        $objImporter = new Importer();
        $res = $objImporter->BUPImport($FileDest, $FileDirectory, $FileName, $ProcessedBy, $ArchiveFileName, $filenameNoExt);
    }
    
    public function PIBimport($FileDest, $FilePIB, $ProcessedBy, $ArchiveFileName, $filenameNoExt)
    {
        $FileDirectory = './../webroot/files/dataPIB/';
        
        //import
        $objImporter = new Importer();
        $res = $objImporter->BUPImport($FileDest, $FileDirectory, $FilePIB, $ProcessedBy, $ArchiveFileName, $filenameNoExt);
    }
    
    public function getArchivedName()
    {
        
    }
}
